package com.tjshop.android;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.toolbox.StringRequest;
import com.tjshop.android.constants.UrlConfigs;
import com.tjshop.android.mcv.GlobeVars;
import com.tjshop.android.util.SharedPreferencesUtil;
import com.tjshop.android.util.WaitProgressDialog;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Activity which displays a login screen to the user, offering registration as
 * well.
 */
public class MyMessageActivity extends BaseActivity {

	private Context mContext;
	private WaitProgressDialog mProgressDialog;

	EditText input_username, input_pass, input_email, input_tel;
	Button login;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_login);
		mContext = this;
		setTheme(R.style.Theme_Sherlock);
		updateCommonReturnBar("我的留言");
//		mProgressDialog = new WaitProgressDialog(this);
//
//		input_username = (EditText) findViewById(R.id.account);
//		input_pass = (EditText) findViewById(R.id.pass);
//
//		login = (Button) findViewById(R.id.login);
//		login.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View arg0) {
//				String username = input_username.getText().toString();
//				if (TextUtils.isEmpty(username)) {
//					Toast.makeText(getApplicationContext(), "请输入用户名",
//							Toast.LENGTH_SHORT).show();
//					input_username.requestFocus();
//					return;
//				}
//				// 验证密码
//				String pass = input_pass.getText().toString();
//				if (TextUtils.isEmpty(pass)) {
//					Toast.makeText(getApplicationContext(), "请输入登陆密码",
//							Toast.LENGTH_SHORT).show();
//					input_pass.requestFocus();
//					return;
//				}
//				login(username, pass);
//			}
//		});

	}

	private void login(final String username, final String pass) {
		mProgressDialog.show("登录中", true, false);

		StringRequest stringRequest = new StringRequest(Method.GET,
				UrlConfigs.URL_LOGIN + "?n=" + username + "&p=" + pass,
				new Response.Listener<String>() {
					@Override
					public void onResponse(String response) {
						JSONObject jsonU;
						try {
							jsonU = new JSONObject(response);
							if (jsonU.getInt("r") == 0) {
								Message message = new Message();
								message.what = 2;
								message.obj = jsonU.getString("t");
								mHandler.sendMessage(message);
							} else {
								Message message = new Message();
								message.what = 3;
								message.obj = jsonU.getString("m");
								mHandler.sendMessage(message);
							}
						} catch (JSONException e) {
							Message message = new Message();
							message.what = 1;
							message.obj = "服务器返回异常！";
							mHandler.sendMessage(message);
							e.printStackTrace();
						}

					}
				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError error) {
						Message message = new Message();
						message.what = 1;
						mHandler.sendMessage(message);

					}

				});

		mQueue.add(stringRequest);
	}

	@Override
	public boolean handleMessage(Message message) {
		switch (message.what) {
		case 1: // 通信异常
			mProgressDialog.dismiss();
			Toast.makeText(mContext, "服务器忙，请稍后再试！", Toast.LENGTH_SHORT).show();
			break;
		case 2: // 登陆成功
			mProgressDialog.dismiss();
			Toast.makeText(mContext, "登陆成功！", Toast.LENGTH_SHORT).show();
			GlobeVars.isLogin = true;
			GlobeVars.userId = input_username.getText().toString();
			GlobeVars.pass = input_pass.getText().toString();
			GlobeVars.token = (String) message.obj;
			SharedPreferencesUtil.save(getApplicationContext(), "userinfo", "bautologin", "true");
			SharedPreferencesUtil.save(getApplicationContext(), "userinfo", "logined", "true");
			SharedPreferencesUtil.save(getApplicationContext(), "userinfo", "username", GlobeVars.userId);
			SharedPreferencesUtil.save(getApplicationContext(), "userinfo", "pass", GlobeVars.pass);
			// Toast.makeText(mContext, GlobeVars.token,
			// Toast.LENGTH_SHORT).show();
			finish();
			break;
		case 3: // 登陆失败
			mProgressDialog.dismiss();
			Toast.makeText(mContext, (String) (message.obj), Toast.LENGTH_SHORT)
					.show();
			break;
		default:
			break;
		}
		return false;
	}

	@Override
	protected void onResume() {
		super.onResume();
	}
}
